package com.wutong.eams.mapper;

import com.wutong.eams.other.ConditionBuilder;
import com.wutong.eams.other.Page;
import com.wutong.eams.utils.DatabaseUtil;

import java.sql.ResultSet;
import java.util.List;

public interface BaseMapper<T> {

    default int insert(T entity) {
        return DatabaseUtil.insert(getTableName(), entity);
    }

    default int update(T entity, ConditionBuilder conditionBuilder) {
        return DatabaseUtil.update(getTableName(), entity, conditionBuilder);
    }

    default int delete(ConditionBuilder conditionBuilder) {
        return DatabaseUtil.delete(getTableName(), conditionBuilder);
    }

    default int count(ConditionBuilder conditionBuilder) {
        return DatabaseUtil.getCount(getTableName(), conditionBuilder);
    }

    default List<T> select(ConditionBuilder conditionBuilder) {
        return DatabaseUtil.query(getTableName(), getEntityClass(), conditionBuilder);
    }

    default T selectOne(ConditionBuilder conditionBuilder) {
        return DatabaseUtil.queryOne(getTableName(), getEntityClass(), conditionBuilder);
    }

    default Page<T> selectPage(ConditionBuilder conditionBuilder, int pageNum, int pageSize) {
        List<T> result = DatabaseUtil.queryByPage(
                getTableName(),
                getEntityClass(),
                conditionBuilder,
                pageNum,
                pageSize
        );
        int total = DatabaseUtil.getCount(getTableName(), conditionBuilder);
        return new Page<>(pageNum, pageSize, total, result);
    }

    default List<T> resultSetToObject(ResultSet resultSet, boolean ignoreNull) {
        return DatabaseUtil.resultSetToObject(resultSet, getEntityClass(), ignoreNull);
    }

    Class<T> getEntityClass();
    String getTableName();
}
